//
//  HttpRequest.m
//  UI_网络请求封装
//
//  Created by dllo on 15/7/16.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HttpRequest.h"

static HttpRequest * httpRequest = nil;
@implementation HttpRequest

+(HttpRequest *)httpRequestHandle
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        httpRequest = [[HttpRequest alloc]init];
    });
    return httpRequest;
}

-(void)loadRequestWithUrl:(NSString *)urlString method:(NSString *)method pramar:(NSString *)pramar
{
    NSString * stringEncode = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL * url = [NSURL URLWithString:stringEncode];
    
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    
    if ([method isEqualToString:@"POST"]) {
        [request setHTTPMethod:@"POST"];
        NSData * bodyData = [pramar dataUsingEncoding:NSUTF8StringEncoding ];
        [request setHTTPBody:bodyData];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        if (data) {
            //不知道是什么类型用id
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            [self.delegate requestData:result];
        }else
        {
            //NSLog(@"请求数据失败");
        }
    }];
}


































@end
